//
//  OKDoctorSelectController.m
//  ZLFastDoctor
//
//  Created by answer on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "OKDoctorSelectController.h"
#import "LSAPIManager.h"
#import "OKSickModel.h"
#import "OKDoctorCell.h"
#import "OKDoctorInfoController.h"
#import "OKDoctorModel.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "LSHTTPManager.h"
#import <MJRefresh/MJRefresh.h>
#import <MJRefresh/MJRefreshComponent.h>

static NSString *OKDoctorSelectControllerIdentifier = @"OKDoctorSelectControllerIdentifier";

@interface OKDoctorSelectController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSArray *data;
@property (strong, nonatomic) NSMutableDictionary *nextPageData;
@end

@implementation OKDoctorSelectController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    self.navigationItem.title = @"选择医生";
    [self.tableView registerClass:[OKDoctorCell class] forCellReuseIdentifier:OKDoctorSelectControllerIdentifier];
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    //self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置普通状态的动画图片
    UIImage *idleImages = [UIImage imageNamed:@"v2_pullRefresh1"];
    UIImage *pullingImages = [UIImage imageNamed:@"v2_pullRefresh2"];
    
    NSArray *array = @[idleImages, pullingImages];
    
    [header setImages:array forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:array forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:array forState:MJRefreshStateRefreshing];
    // 设置header
    self.tableView.mj_header = header;
}

- (void)loadNewData {
    [NSThread sleepForTimeInterval:1.0];
    [self.tableView.mj_header endRefreshing];
}

- (void)loadData {
    
    [OKSickModel requestDoctorWithParams:self.params completion:^(NSArray *result) {
        self.data = result;
       // NSLog(@"%@", result);
        [self.tableView reloadData];
    }];
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OKDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:OKDoctorSelectControllerIdentifier forIndexPath:indexPath];
    cell.model = self.data[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OKDoctorModel *model = self.data[indexPath.row];
    
    [SVProgressHUD showWithStatus:@"加载中"];
    [self pushWithDocrorID:model.doctor_id];
}

- (void)pushWithDocrorID:(NSString *)doctorID {
    
    [SVProgressHUD showWithStatus:@"加载中"];

    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("com.xiaofan", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSDictionary *parameters = @{@"doctor_id":@300000315};
        [[LSHTTPManager sharedManager] POST:@"doctorReceivingSetting.json.php" parameters:parameters comletionHandle:^(id responseObject, NSError *error) {
            NSDictionary *result = responseObject[@"data"];
            
            [self.nextPageData setObject:result[@"receiving_setting_extra"] forKey:@"receiving_setting_extra"];
            [self.nextPageData setObject:result[@"receiving_settings"] forKey:@"receiving_settings"];
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSDictionary *parameters = @{@"doctor_id":@300000315};
        [[LSHTTPManager sharedManager] POST:@"getIntroduction.json.php" parameters:parameters comletionHandle:^(id responseObject, NSError *error) {
            NSDictionary *result = responseObject[@"data"];
            
            [self.nextPageData setObject:result[@"introduction"] forKey:@"introduction"];
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSDictionary *parameters = @{@"doctor_id":@300000315};
        [[LSHTTPManager sharedManager] POST:@"doctorDuties.json.php" parameters:parameters comletionHandle:^(id responseObject, NSError *error) {
            
            dispatch_group_leave(group);
            
        }];
    });
    
    
    dispatch_group_notify(group, queue, ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            OKDoctorInfoController *vc = [[OKDoctorInfoController alloc] init];
            vc.doctor_id = doctorID;
            vc.data = self.nextPageData.copy;

            [self.navigationController pushViewController:vc animated:YES];
        });
    });
    
    
}

- (NSMutableDictionary *)nextPageData {
    if (_nextPageData == nil) {
        _nextPageData = [NSMutableDictionary dictionary];
    }
    return _nextPageData;
}


@end
